import { createRouter, createWebHistory } from "vue-router";
import { useTokenStore } from "@/stores/modules/user";
import Layout from "@/layout/index.vue";
import { Tools } from "@element-plus/icons-vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/inventory/InventoryQuery",
      hidden: true,
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login/LoginPage.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("@/views/Login/RegisterPage.vue"),
    },
    {
      path: "/quality",
      component: () => import("@/layout/index.vue"),
      name: "Quality",
      meta: {
        title: "质量管理",
        icon: "Check",
      },
      children: [
        {
          path: "inspection",
          component: () => import("@/views/quality/inspection.vue"),
          name: "Inspection",
          meta: { title: "来料检验" },
        },
        {
          path: "process-control",
          component: () => import("@/views/quality/process-control.vue"),
          name: "ProcessControl",
          meta: { title: "过程检验" },
        },
        {
          path: "final-inspection",
          component: () => import("@/views/quality/final-inspection.vue"),
          name: "FinalInspection",
          meta: { title: "成品检验" },
        },
        {
          path: "defect-management",
          component: () => import("@/views/quality/defect-management.vue"),
          name: "DefectManagement",
          meta: { title: "不合格品管理" },
        },
        {
          path: "quality-analysis",
          component: () => import("@/views/quality/quality-analysis.vue"),
          name: "QualityAnalysis",
          meta: { title: "质量分析" },
        },
      ],
    },
    // {
    //   path: "/",
    //   component: () => import("@/layout/index.vue"),
    //   redirect: "/dashboard",
    //   children: [
    //     {
    //       path: "dashboard",
    //       name: "dashboard",
    //       component: () => import("@/views/dashboard/index.vue"),
    //     },
    //     {
    //       path: "users",
    //       name: "users",
    //       component: () => import("@/views/users/index.vue"),
    //     },
    //     {
    //       path: "materials",
    //       name: "materials",
    //       component: () => import("@/views/materials/index.vue"),
    //     },
    //     {
    //       path: "finance",
    //       name: "finance",
    //       component: () => import("@/views/finance/index.vue"),
    //     },
    //     {
    //       path: "approvals",
    //       name: "approvals",
    //       component: () => import("@/views/approvals/index.vue"),
    //     },
    //   ],
    // },
    {
      path: "/receipt",
      component: Layout,
      meta: { title: "物料管理", icon: "Document" },
      children: [
        {
          path: "MaterialReceiptMaintenance",
          name: "MaterialReceiptMaintenance",
          component: () =>
            import("@/views/receipt/MaterialReceiptMaintenance.vue"),
          meta: { title: "收料维护" },
        },
        // {
        //   path: 'MaterialIssuesMaintenance',
        //   name: 'MaterialIssuesMaintenance',
        //   component: () => import('@/views/receipt/MaterialIssuesMaintenance.vue'), // TODO: 待实现
        //   meta: { title: '发料维护' }
        // },
        // {
        //   path: 'MaterialTransferMaintenance',
        //   name: 'MaterialTransferMaintenance',
        //   component: () => import('@/views/receipt/MaterialTransferMaintenance.vue'), // TODO: 待实现
        //   meta: { title: '调拨维护' }
        // },
        // {
        //   path: 'StockCountMaintenance',
        //   name: 'StockCountMaintenance',
        //   component: () => import('@/views/receipt/StockCountMaintenance.vue'), // TODO: 待实现
        //   meta: { title: '盘点维护' }
        // }
      ],
    },
    {
      path: "/inventory",
      component: Layout,
      meta: { title: "生产管理", icon: "Box" },
      children: [
        {
          path: "InventoryQuery",
          name: "InventoryQuery",
          component: () => import("@/views/inventory/InventoryQuery.vue"),
          meta: { title: "库存查询" },
        },
        {
          path: "InventoryAdjustment",
          name: "InventoryAdjustment",
          component: () => import("@/views/inventory/InventoryAdjustment.vue"),
          meta: { title: "生产库存调整" },
        },
        {
          path: "InventoryFreeze",
          name: "InventoryFreeze",
          component: () => import("@/views/inventory/InventoryFreeze.vue"),
          meta: { title: "生产计划和排产" },
        },
        {
          path: "InventoryWarning",
          name: "InventoryWarning",
          component: () => import("@/views/inventory/InventoryWarning.vue"),
          meta: { title: "分派任务" },
        },
        {
          path: "InventoryAnalysis",
          name: "InventoryAnalysis",
          component: () => import("@/views/inventory/InventoryAnalysis.vue"),
          meta: { title: "库存分析" },
        },
        {
          path: "InventoryLedger",
          name: "InventoryLedger",
          component: () => import("@/views/inventory/InventoryLedger.vue"),
          meta: { title: "库存台账" },
        },
        {
          path: "InventorySnapshot",
          name: "InventorySnapshot",
          component: () => import("@/views/inventory/InventorySnapshot.vue"),
          meta: { title: "生产绩效" },
        },
      ],
    },
    {
      path: "/base",
      component: Layout,
      meta: { title: "基础数据", icon: "Setting" },
      children: [
        {
          path: "MaterialManagement",
          name: "MaterialManagement",
          component: () => import("@/views/base/MaterialManagement.vue"),
          meta: { title: "物料管理" },
        },
        {
          path: "WarehouseManagement",
          name: "WarehouseManagement",
          component: () => import("@/views/base/WarehouseManagement.vue"),
          meta: { title: "仓库管理" },
        },
        {
          path: "SupplierManagement",
          name: "SupplierManagement",
          component: () => import("@/views/base/SupplierManagement.vue"),
          meta: { title: "供应商管理" },
        },
        {
          path: "CustomerManagement",
          name: "CustomerManagement",
          component: () => import("@/views/base/CustomerManagement.vue"),
          meta: { title: "客户管理" },
        },
        {
          path: "UnitManagement",
          name: "UnitManagement",
          component: () => import("@/views/base/UnitManagement.vue"),
          meta: { title: "计量单位" },
        },
        // {
        //   path: 'MaterialCategoryManagement',
        //   name: 'MaterialCategoryManagement',
        //   component: () => import('@/views/base/MaterialCategoryManagement.vue'), // TODO: 待实现
        //   meta: { title: '物料分类' }
        // },
        // {
        //   path: 'WarehouseAreaManagement',
        //   name: 'WarehouseAreaManagement',
        //   component: () => import('@/views/base/WarehouseAreaManagement.vue'), // TODO: 待实现
        //   meta: { title: '仓库区域' }
        // },
        // {
        //   path: 'LocationTypeManagement',
        //   name: 'LocationTypeManagement',
        //   component: () => import('@/views/base/LocationTypeManagement.vue'), // TODO: 待实现
        //   meta: { title: '库位类型' }
        // },
        // {
        //   path: 'ReceiptTypeManagement',
        //   name: 'ReceiptTypeManagement',
        //   component: () => import('@/views/base/ReceiptTypeManagement.vue'), // TODO: 待实现
        //   meta: { title: '单据类型' }
        // }
      ],
    },
    {
      path: "/equipment",
      component: Layout,
      meta: { title: "设备管理", icon: "Tools" },
      children: [
        {
          path: "EquipmentLedger",
          name: "EquipmentLedger",
          component: () => import("@/views/equipment/EquipmentLedger.vue"),
          meta: { title: "设备台账管理" },
        },
        {
          path: "EquipmentInspection",
          name: "EquipmentInspection",
          component: () => import("@/views/equipment/EquipmentInspection.vue"),
          meta: { title: "设备点检管理" },
        },
        {
          path: "EquipmentMaintenance",
          name: "EquipmentMaintenance",
          component: () => import("@/views/equipment/EquipmentMaintenance.vue"),
          meta: { title: "设备维保管理" },
        },
        {
          path: "EquipmentFailure",
          name: "EquipmentFailure",
          component: () => import("@/views/equipment/EquipmentFailure.vue"),
          meta: { title: "设备故障管理" },
        },
        {
          path: "EquipmentMonitoring",
          name: "EquipmentMonitoring",
          component: () => import("@/views/equipment/EquipmentMonitoring.vue"),
          meta: { title: "设备运行监控" },
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  const tokenStore = useTokenStore();
  const token = tokenStore.accessToken;

  const whiteList = ["/login", "/register"];

  if (whiteList.includes(to.path)) {
    if (token && to.path === "/login") {
      next("/dashboard");
    } else {
      next();
    }
  } else {
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
